﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;


/// <summary>
/// 学校考试管理
/// </summary>
public partial class School_Manage_SchoolSelfManage : System.Web.UI.Page
{
 //   public static List<ExamsSchool> listSchoolExam = new List<ExamsSchool>();
    public int currentOpenEXAM = 0;
    public int currSchoolYear = 0, currTerm = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // InitYearAndTerm();
            currentOpenEXAM = SchoolCookie.ExamID;
            Bind();
        }
    }
    
    //private void InitYearAndTerm()
    //{
    //    if (CommonScore.SelectSohoolInfo() != null)
    //    {
    //        currSchoolYear = CommonScore.SelectSohoolInfo().SchoolYear;
    //        currTerm = CommonScore.SelectSohoolInfo().SchoolTerm;
    //    }
    //}

    protected int FindCount(int id)
    {
        IExamClassSchoolLogic eduExamLogic = Container<IExamClassSchoolLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("ExamID",id);
        ht.Add("SchoolMark", ContainerBasePage.SchoolMark);
        return eduExamLogic.FindCount(ht);
    }
    //初始化 repeat控件，集成显示教师关联信息
    protected void btnBind_Click(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        currentOpenEXAM = SchoolCookie.ExamID;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", CommonScore.GetSchoolMark);

        IExamsSchoolLogic eduExamLogic = Container<IExamsSchoolLogic>.Instance;
        List<ExamsSchool> list = (List<ExamsSchool>)eduExamLogic.FindAll(ht);
        CommonScore.ConvertSchoolExamHtmlCode(ref list);
      //  listSchoolExam = list;
        //Repeater1.DataSource = list;

        List<ExamsSchool> targetList = new List<ExamsSchool>();
        currSchoolYear = CommonScore.SelectSohoolInfo().SchoolYear;
        currTerm = CommonScore.SelectSohoolInfo().SchoolTerm;
        int year = 0, term = 0;
        if(string.IsNullOrEmpty(txtYear.Text))
        {
            if (currSchoolYear > 0 && currTerm > 0)
            {
                year = currSchoolYear;
                term = currTerm;
            }
        }
        else
        {
            int.TryParse(txtYear.Text, out year);
            int.TryParse(txtTerm.Text, out term);
        }
        if (year > 0)
        {
            if (term > 0)
            {
                foreach (ExamsSchool item in list)
                {
                    if (item.SchoolYear == year && item.Term == term)
                        targetList.Add(item);
                }
            }
            else
            {
                foreach (ExamsSchool item in list)
                {
                    if (item.SchoolYear == year)
                        targetList.Add(item);
                }
            }
            Repeater1.DataSource = targetList;
        }
        else
        {
            Repeater1.DataSource = list;
        }
        Repeater1.DataBind();
    }


    /// <summary>
    /// 删除校级考试
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int DelSchoolExam(int examId)
    {
        IExamsSchoolLogic shoolExamLogic = Container<IExamsSchoolLogic>.Instance;
        int n = shoolExamLogic.DelShoolExam(examId,CommonScore.GetSchoolMark);
        return n;
    }
 

    /// <summary>
    /// 获取对应考试的科目
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static List<ExamSubjecySchool> GetSubjectsBySchoolExam(int examId)
    {
        IExamSubjecySchoolLogic subjectLogic = Container<IExamSubjecySchoolLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("ExamID", examId);
        table.Add("SchoolMark", CommonScore.GetSchoolMark);

        List<ExamSubjecySchool> list = (List<ExamSubjecySchool>)subjectLogic.FindAll(table);
        return list;
    }


   
    protected void btnSelect_Click(object sender, EventArgs e)
    {
        int year = -1, term = -1, grade = -1;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", CommonScore.GetSchoolMark);

        string s = "";
        s = txtYear.Text;
        if (!String.IsNullOrEmpty(s))
        {
            year = Convert.ToInt32(s);
            if (year > 0)
                ht.Add("SchoolYear", year);
        }
        s = txtTerm.Text;
        if (!String.IsNullOrEmpty(s))
        {
            term = Convert.ToInt32(s);
            if(term > 0)
                ht.Add("Term", term);
        }
        s = txtGrade.Text;
        if (!String.IsNullOrEmpty(s))
        {
            grade = Convert.ToInt32(s);
            if (grade > 0)
                ht.Add("Grade", grade);
        }

        IExamsSchoolLogic eduExamLogic = Container<IExamsSchoolLogic>.Instance;
        List<ExamsSchool> listSchoolExam = (List<ExamsSchool>)eduExamLogic.FindAll(ht);
        CommonScore.ConvertSchoolExamHtmlCode(ref listSchoolExam);

        //string[] arr = new string[] { "0", "0"};
        //if (term > 0)
        //    arr[0] = "1";
        //if (grade > 0)
        //    arr[1] = "1";


        //List<ExamsSchool> newList = new List<ExamsSchool>();
        //List<ExamsSchool> targetList = new List<ExamsSchool>();

        //if (year > 0)
        //{
        //    foreach (ExamsSchool item in listSchoolExam)
        //    {
        //        if (item.SchoolYear == year)
        //            targetList.Add(item);
        //    }
        //}
        //else
        //{
        //    targetList = listSchoolExam;
        //}
        //newList = CommonScore.FilterTermGradeState(String.Join("", arr), targetList, term, grade);
        currentOpenEXAM = SchoolCookie.ExamID;
        Repeater1.DataSource = listSchoolExam;
        Repeater1.DataBind();
    }

    private void SetCookie(string examId, string examName, string grade, string classType, string canupload)
    {
        int nExamId = 0, nGrade = 0, nClassType = 0, canup = -1;
        if (!String.IsNullOrEmpty(examId))
        {
            if (int.TryParse(examId, out nExamId))
                nExamId = Convert.ToInt32(examId);
        }
        if (!String.IsNullOrEmpty(grade))
        {
            if (int.TryParse(grade, out nGrade))
                nGrade = Convert.ToInt32(grade);
        }
        if (!String.IsNullOrEmpty(classType))
        {
            if (int.TryParse(classType, out nClassType))
                nClassType = Convert.ToInt32(classType);
        }
        if (!String.IsNullOrEmpty(canupload))
        {
            if (int.TryParse(canupload, out canup))
                canup = Convert.ToInt32(canupload);
        }
        if (nExamId > 0 && nGrade > 0)
        {
            currentOpenEXAM = nExamId;
            SchoolCookie.ClearExamInfo();
            SchoolCookie.SetOpenedExam(0, nExamId, examName, nGrade, nClassType, 0);

            //Repeater1.DataSource = listSchoolExam;
            //Repeater1.DataBind();
            Bind();

        }
    }

    /// <summary>
    /// 设置cookie
    /// </summary>
    /// <param name="examId"></param>
    /// <param name="examName"></param>
    /// <param name="grade"></param>
    /// <param name="classType"></param>
    [System.Web.Services.WebMethod]
    public static void SetCookie2(string examId, string examName, string grade, string classType, string canupload)
    {
        int nExamId = 0, nGrade = 0, nClassType = 0, canup = -1;
        if (!String.IsNullOrEmpty(examId))
        {
            if (int.TryParse(examId, out nExamId))
                nExamId = Convert.ToInt32(examId);
        }
        if (!String.IsNullOrEmpty(grade))
        {
            if (int.TryParse(grade, out nGrade))
                nGrade = Convert.ToInt32(grade);
        }
        if (!String.IsNullOrEmpty(classType))
        {
            if (int.TryParse(classType, out nClassType))
                nClassType = Convert.ToInt32(classType);
        }
        if (!String.IsNullOrEmpty(canupload))
        {
            if (int.TryParse(canupload, out canup))
                canup = Convert.ToInt32(canupload);
        }
        if (nExamId > 0 && nGrade > 0)
        {
          //  currentOpenEXAM = nExamId;
            SchoolCookie.ClearExamInfo();
            SchoolCookie.SetOpenedExam(0, nExamId, examName, nGrade, nClassType, canup);
        }
    }

    /// <summary>
    /// 获取打开的考试名
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    public string GetOpenedExamName()
    {
        string examName  = SchoolCookie.ExamName;
        return examName;
    }



    #region 学年、学期、年级、状态的转换
    public string ConvertGrade(int grade)
    {
        return CommonScore.ConvertGradeNum(grade);
    }
    public string ConvertTerm(short term)
    {
        return CommonScore.ConvertTerm(term);
    }
    public string ConvertClassType(int classType)
    {
        return CommonScore.ConvertClassType(classType);
    }
    public string ConvertState(int state)
    {
        return CommonScore.ConvertTestStatus(state);
    }
    #endregion


    protected void btnSetCookie_Click(object sender, EventArgs e)
    {
        SetCookie(txtExamId.Text,txtExamName.Text,txtGrade.Text,txtClassType.Text,txtCanUpload.Text);
    }


    /// <summary>
    /// 验证此考试是否录入数据
    /// </summary>
    /// <param name="examId"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ValidateIsInputScore(int examId)
    {
        IExamDetailSchoolLogic logic = Container<IExamDetailSchoolLogic>.Instance;
        Hashtable table = new Hashtable();
        table.Add("SchoolMark", Common.GetConstSchoolMark());
        table.Add("ExamID", examId);

        int n = logic.FindCount(table);
        return n;
    }
}
